package com.example.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.example.demo.config.AlipayConfig1;
import com.example.demo.entity.AlipayBean;
import com.example.demo.service.AlipayService;
import org.springframework.stereotype.Service;

@Service
public class AlipayServiceImpl implements AlipayService {

    @Override
    public String alipay(AlipayBean alipayBean) throws AlipayApiException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                AlipayConfig1.gatewayUrl,
                AlipayConfig1.appId,
                AlipayConfig1.privateKey,
                "json",
                AlipayConfig1.charset,
                AlipayConfig1.publicKey,
                AlipayConfig1.signType);
        //设置请求参数
        //  AlipayTradePayModel
        // AlipayTradeAppPayRequest alipayRequest = new AlipayTradeAppPayRequest();
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();   //支付请求  WapPay：手机移动端或平板支付；AppPay：小程序；PagePay：PC网页版
        //AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setNotifyUrl(AlipayConfig1.notify_url);
        alipayRequest.setReturnUrl(AlipayConfig1.returnUrl);

        System.out.println(JSON.toJSONString(alipayBean));
        alipayRequest.setBizContent(JSON.toJSONString(alipayBean));  //把对象转换成JSON字符串
        //请求
        //  AlipayTradeAppPayResponse response = alipayClient.sdkExecute(alipayRequest);
        //  AlipayTradeWapPayResponse response  =    alipayClient.pageExecute(alipayRequest);
        //  String result = response.getBody();
        String result = alipayClient.pageExecute(alipayRequest).getBody();

        System.out.println(result+"      Service里的东西");
        return result;
    }
}
